package com.suxin.module.http.callback;

import com.suxin.module.bean.MyPoint;
import com.suxin.module.http.response.MyDetailResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.concurrent.FutureCallback;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname BatchPutHttpResponseCallback
 * @Description [ 批量put的回调响应接口 ]
 * @Author Tang
 * @Date 2020/10/17 10:25
 * @Created by ASUS
 */
@Slf4j
public class MyBatchPutHttpResponseCallback implements FutureCallback<HttpResponse> {

    private AtomicInteger unCompleteTask;

    private BatchPutCallBack batchPutCallBack;

    private List<MyPoint> myPointList;

    public MyBatchPutHttpResponseCallback(BatchPutCallBack batchPutCallBack,
                                          List<MyPoint> myPointList) {
        this.batchPutCallBack = batchPutCallBack;
        this.myPointList = myPointList;
    }

    @Override
    public void completed(HttpResponse response) {

        log.info("还剩余任务数是：{}",this.unCompleteTask.decrementAndGet());

        //1.解析封装数据

        //2.回调接口
        this.batchPutCallBack.response(myPointList, new MyDetailResult());

    }

    @Override
    public void failed(Exception ex) {

    }

    @Override
    public void cancelled() {

    }

    public interface BatchPutCallBack {

        /**
         * 成功的回调
         * @param points  传入的内容列表
         * @param myDetailResult 返回值
         */
        void response(List<MyPoint> points, MyDetailResult myDetailResult);


        /**
         * 失败
         * @param points
         * @param myDetailResult
         */
        void responseError(List<MyPoint> points, MyDetailResult myDetailResult);

        /***
         * 在发生错误是回调
         * @param points 数据点
         * @param e 异常
         */
        void failed(List<MyPoint> points, Exception e);
    }

}